﻿package
	{
	import flash.display.MovieClip;
	import flash.text.*;
	import flash.events.*;
	
	public class Inventory extends MovieClip
		{
		var slots:Array;
		var ghostItem:MovieClip;
		var draggedItem:MovieClip;
		var sourceSlot:InventorySlot;
		var player:Player;
		
		public function Inventory(player:Player)
			{
			this.slots = new Array();
			this.ghostItem = null;
			this.draggedItem = null;
			this.player = player;
			
			for (var i = 0; i<9; i++)
				{
				this.slots.push(new InventorySlot());
				this.addChild(slots[i]);
				this.slots[i].x = (i%3)*75+50;
				this.slots[i].y = 75*int(i/3)+200;
				}
			this.addEventListener(Event.ADDED_TO_STAGE, addEvents);
			}
		
		public function addEvents(e:Event)
			{
			this.addEventListener(MouseEvent.CLICK, getMouseDown);
			}
		
		public function getMouseDown(e:MouseEvent)
			{
			if (this.ghostItem && e.target.dropTarget != null)
				{
				var dropTarget = e.target.dropTarget.parent;
				var droppedItem:Item;
				
				//Drop item on map
				if (dropTarget is Map)
					{
					dropItem();
					}
				else if (isInventorySlot(dropTarget))
					{
					dropTarget=isInventorySlot(dropTarget);
					droppedItem = this.sourceSlot.item;
					//Switch item to an empty space
					if (dropTarget.quantity == 0)
						{
						this.sourceSlot.removeItem();
						dropTarget.addItem(droppedItem);
						}
					//Switch item to an occupied space
					else if (dropTarget.quantity > 0)
						{
						if (this.sourceSlot != dropTarget)
							swapItems(this.sourceSlot, dropTarget)
						}
					}
				this.draggedItem.alpha = 1;
				this.ghostItem.stopDrag();
				this.removeChild(this.ghostItem);
				this.ghostItem = null;
				}
			else
				{
				var clicked:InventorySlot = isInventorySlot(e.target);
				
				if (clicked && clicked.item)
					{
					this.sourceSlot = clicked;
					this.draggedItem = clicked.item.itemIcon as MovieClip;
					this.draggedItem.alpha = 0.7;
					this.ghostItem = MainClass.cloneMC(this.draggedItem);
					this.ghostItem.x = e.stageX;
					this.ghostItem.y = e.stageY;
					this.addChild(this.ghostItem);
					this.ghostItem.startDrag();
					}
				}
			}
		
		public function isInventorySlot(slot:Object):InventorySlot
			{
			var result:InventorySlot = null;
			
			if (slot is InventorySlot)
				result = slot as InventorySlot;
			else if (slot.parent is InventorySlot)
				result = slot.parent as InventorySlot;
			
			return result;
			}
		
		public function pickUpItem(item:Item)
			{
			var i:int;
			
			if (item.maxStack == 1)
				{
				i = getFreeSlot();
				if (i >= 0)
					slots[i].addItem(item);
				else
					trace ("no stack Inventory is full...");
				}
			else if (item.maxStack > 1)
				{
				i = getFreeStackable(item);
				if (i >= 0)
					slots[i].addItem(item);
				else 
					{
					i = getFreeSlot();
					if (i >= 0)
						slots[i].addItem(item);
					else
						trace ("stack Inventory is full...");
					}
				}
			else
				trace (item.itemName + " has incorect max stack value...");
			}
		
		public function swapItems(slotA:InventorySlot, slotB:InventorySlot)
			{
			var itemA:Item = slotA.item;
			var itemB:Item = slotB.item;
			
			slotA.removeItem();
			slotB.removeItem();
			slotA.addItem(itemB);
			slotB.addItem(itemA);
			}
		
		public function dropItem()
			{
			var equipClone:Equipment = (this.sourceSlot.item as Equipment).cloneEquip();
			player.parent.addChildAt(equipClone,1);
			equipClone.x = player.x + Player.signedRand(150);
			equipClone.y = player.y + Player.signedRand(150);
			(this.sourceSlot.item as Equipment).unEquip(player);
			this.sourceSlot.removeItem();
			}
		
		public function getFreeSlot():int
			{
			var freeIndex = -1;
			
			for (var i in slots)
				{
				if (slots[i].quantity == 0)
					{
					freeIndex = i;
					break;
					}
				}
			return freeIndex;
			}
		
		public function getFreeStackable(itemToStack:Item):int
			{
			var freeIndex = -1;
			
			for (var i in slots)
				{
				if (slots[i].item != null)
					{
					if (slots[i].quantity < itemToStack.maxStack && slots[i].item.itemName == itemToStack.itemName )
						{
						freeIndex = i;
						break;
						}
					}
				}
			return freeIndex;
			}
		}
		
	}